package com.qxzn.module.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.qxzn.app.bean.RepeatBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;
import com.qxzn.module.home.adapter.RemindRepeatAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class RemindRepeatActivity extends BaseActivity {

    @BindView(R2.id.rv_remind_repeat)
    RecyclerView rvRemindRepeat;
    private List<RepeatBean> dataList = new ArrayList<>();
    private RemindRepeatAdapter remindRepeatAdapter;
    private String repeatJson = "[{\"id\":8,\"type\":3,\"name\":\"只响一次\"},{\"id\":7,\"type\":1,\"name\":\"周日\"},{\"id\":1,\"type\":1,\"name\":\"周一\"},{\"id\":2,\"type\":1,\"name\":\"周二\"},{\"id\":3,\"type\":1,\"name\":\"周三\"},{\"id\":4,\"type\":1,\"name\":\"周四\"},{\"id\":5,\"type\":1,\"name\":\"周五\"},{\"id\":6,\"type\":1,\"name\":\"周六\"}]";
    private int repeatType;
    private String repeatValue = "";

    @Override
    protected int initLayout() {
        return R.layout.activity_remind_repeat;
    }

    @Override
    protected void initView() {
        rvRemindRepeat.setLayoutManager(new LinearLayoutManager(this));
        remindRepeatAdapter = new RemindRepeatAdapter(this, dataList);
        rvRemindRepeat.setAdapter(remindRepeatAdapter);
        remindRepeatAdapter.setItemClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RepeatBean item = (RepeatBean) v.getTag();
                item.setCheck(!item.isCheck());
                if (item.isCheck()) {
                    if (item.getType() == 3) {
                        for (int i = 1; i < dataList.size(); i++) {
                            dataList.get(i).setCheck(false);
                        }
                    } else {
                        dataList.get(0).setCheck(false);
                    }
                }
                remindRepeatAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("重复");
        iv_back.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        repeatType = getIntent().getIntExtra(Constant.PutExtraKey.REPEAT_TYPE, -1);
        repeatValue = getIntent().getStringExtra(Constant.PutExtraKey.REPEAT_VALUE);
        dataList.addAll(JSON.parseArray(repeatJson, RepeatBean.class));
        if (repeatType != -1) {
            if (repeatType == 0) {
                for (int i = 1; i < dataList.size(); i++) {
                    dataList.get(i).setCheck(true);
                }
            } else if (repeatType == 1) {
                if (repeatValue != null) {
                    String[] repeatValues = repeatValue.split(",");
                    if (repeatValues != null && repeatValues.length > 0) {
                        for (RepeatBean repeatBean : dataList) {
                            for (String value : repeatValues) {
                                if ((repeatBean.getId() + "").equals(value)) {
                                    repeatBean.setCheck(true);
                                }
                            }
                        }
                    }
                }
            } else if (repeatType == 3) {
                dataList.get(0).setCheck(true);
            }
        }
        remindRepeatAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_back) {
            AppUtil.showLog(JSON.toJSONString(dataList));
            int i = 0;
            repeatValue = "";
            for (RepeatBean repeatBean : dataList) {
                if (repeatBean.isCheck()) {
                    if (repeatBean.getType() == 3) {
                        repeatType = repeatBean.getType();
                        repeatValue = "";
                        break;
                    } else {
                        i++;
                        repeatType = repeatBean.getType();
                        if ("".equals(repeatValue)) {
                            repeatValue = "" + repeatBean.getId();
                        } else {
                            repeatValue += "," + repeatBean.getId();
                        }
                    }
                }
            }
            if (i == 7) {
                repeatType = 0;
            }
            Intent intent = new Intent().putExtra(Constant.PutExtraKey.REPEAT_TYPE, repeatType).putExtra(Constant.PutExtraKey.REPEAT_VALUE, repeatValue);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
